package com.fmi.cloud.biz;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;

import com.fmi.cloud.R;
import com.fmi.cloud.act.MainActivity;
import com.fmi.cloud.act.SplashActivity;
import com.fmi.cloud.app.YcApp;
import com.fmi.cloud.util.ImageDownloader;
import com.fmi.cloud.util.IntentHelper;
import com.fmi.cloud.util.OrangeDataHandler;
import com.fmi.cloud.util.OrangeErrorHandler;
import com.fmi.cloud.util.StringUtil;
import com.fmi.cloud.util.VolleyHttpUtil;
import com.fmi.cloud.util.YcToastHelper;
import com.fmi.cloud.view.LoadDialog;
import com.fmi.thirdsdk.jpush.TagAliasOperatorHelper;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by zv on 2017-12-5.
 */

public class FmHttpBiz {

    public static String getStr(Context context, int id) {
        return context.getResources().getString(id);
    }

    public static void getVersion(final Context context) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("phoneType", "Android");
        map.put("type", context.getResources().getString(R.string.type));
        VolleyHttpUtil.getInstance().postWithCallback(getStr(context, R.string.path_version), map, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                boolean isOk = OrangeErrorHandler.getInstance().isSuccess(response);
                if (isOk) {
                    Map<String, Object> map = OrangeDataHandler.getMapWithEntityNoGson(response);
                    if (map != null && map.size() > 0) {
                        new VersionUtil(context).handleVersion(map);
                    }
                }
            }
        });
    }

    public static void jumpMain(Context ctx) {
        if (FmHttpBiz.getType(ctx).equals(YcApp.TYPE_BROKER)) {
            IntentHelper.jump(ctx, MainActivity.class, "type", "login");
        } else {
            IntentHelper.getInstances().getIntent(ctx, MainActivity.class);
        }
        ((Activity) ctx).finish();
    }


    public static void getSplashImage(Context context, final ImageView imageView) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("pageIndex", "1");
        map.put("pageSize", "10");
        map.put("bannerStatus", "1");
        map.put("bannerType", "闪屏");
        VolleyHttpUtil.getInstance().postWithCallback(getStr(context, R.string.path_slash), map, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                boolean isOk = OrangeErrorHandler.getInstance().isSuccess(response, true);
                if (isOk) {
                    List<Map<String, Object>> list = OrangeDataHandler.getListFromJson(response, "pageEntity", null, null);
                    if (list != null && list.size() > 0) {
                        Map<String, Object> map = list.get(0);
                        String url = (String) map.get("bannerImageUrl");
                        ImageDownloader.getImageDownloader().download(url, imageView);
                    }
                }
            }
        });
    }

    public static String getType(Context context) {
        return ((YcApp) context.getApplicationContext()).getAppType();
    }


}
